package cn.tworice.scheduled;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/tasks")
public class TaskController {

    @Resource
    private DynamicTaskService dynamicTaskService;

//    @PostMapping("/add")
//    public String addTask(@RequestParam String taskId, @RequestParam String cronExpression) {
//        dynamicTaskService.addTask(taskId, () -> System.out.println(taskId + " is running..."), cronExpression);
//        return "Task added";
//    }
//
//    @PostMapping("/scheduleAtTimestamp")
//    public String scheduleTaskAt(@RequestParam String taskId, @RequestParam long timestamp) {
//        dynamicTaskService.addTask(taskId, () -> System.out.println(taskId + " is running at " + new Date(timestamp)), timestamp);
//        return "Task scheduled at " + new Date(timestamp);
//    }

    @DeleteMapping("/remove")
    public String removeTask(@RequestParam String taskId) {
        dynamicTaskService.removeTask(taskId);
        return "Task removed";
    }

//    @PutMapping("/update")
//    public String updateTask(@RequestParam String taskId, @RequestParam String cronExpression) {
//        dynamicTaskService.updateTask(taskId, () -> System.out.println(taskId + " is updated and running..."), cronExpression);
//        return "Task updated";
//    }
}

